<template>

	<uni-file-picker ref="filePicker1" :value="fileList" :limit="selectNum" :fileMediatype="fileType"
		@select="selectFile" :title="title" @progress="progress" @success="success" @fail="fail" @delete="removeFile"
		:sourceType="['album', 'camera']"></uni-file-picker>




</template>

<script>
	import {
		HOST_H5,
		filePreviewUrl
	} from '@/config/env.js'
	import
	uploadApi
	from '@/common/api/upload.js'
	export default {
		name: "file-upload",
		data() {
			return {
				fileList: [],
				selectNum: 1,
			};
		},
		props: {
			value: {
				type: String,
				default: null,
			},
			// 图片数量限制
			limit: {
				type: Number,
				default: 5,
			},

			// 文件类型, 例如['png', 'jpg', 'jpeg']
			fileType: {
				type: String,
				default: 'image',
			},
			title: {
				type: String,
				default: '',
			},
			// 是否显示提示
			// isShowTip: {
			// 	type: Boolean,
			// 	default: true
			// }
		},
		watch: {
			fileList: {
				handler(val) {

					if (val.length == 0) {
						this.selectNum = 1
					} else if (val.length >= this.limit) {
						this.selectNum = this.limit
					} else {
						this.selectNum = this.selectNum + 1
					}


				},
				deep: true,
				immediate: true
			},
			value: {
				handler(val) {
					if (val) {
						debugger
						const list = Array.isArray(val) ? val : this.value.split(',');

						this.fileList = list.map(item => {
							debugger
							if (typeof item === "string") {
								var fileName = item.substring(item.lastIndexOf("/") + 1, item.length)
								if (item.indexOf(filePreviewUrl) === -1) {
									item = {
										name: fileName,
										url: filePreviewUrl + item
									};
								} else {
									item = {
										name: fileName,
										url: item
									};
								}
							}
							return item;
						});
					} else {
						this.fileList = [];
						return [];
					}
				},
				deep: true,
				immediate: true
			}
		},
		methods: {
			// 对象转成指定字符串分隔
			listToString(list, separator) {
				let strs = "";
				separator = separator || ",";
				for (let i in list) {
					if (list[i].url) {
						debugger
						strs += list[i].url.replace(filePreviewUrl, "") + separator;
						//strs += list[i].url + separator;
					}
				}
				return strs != '' ? strs.substr(0, strs.length - 1) : '';
			},
			// 获取上传状态
			selectFile(e) {
				console.log(e)


				console.log(e, this.fileList)
				const tempFilePaths = e.tempFilePaths;
				const imgUrl = tempFilePaths[0]

				var fileExtIndex = 1
				const fileName = e.tempFiles[0].name
				fileExtIndex = fileName.lastIndexOf(".")


				// const fileName = e.tempFiles[0].name;
				// extname = fileName. splice(".")
				// alert("cloudPath=" + tempFile.cloudPath)
				// alert("extname=" + tempFile.extname)
				// alert("name=" + tempFile.name)


				uni.showLoading({
					title: '上传中...'
				});


				uploadApi.upload({
					url: '/wx/yingyong/common/upload',
					filePath: imgUrl,
					data: {
						"vailExt": "1",
						"fileExt": fileExtIndex == -1 ? 'mp4' : ''
					}
				}).then((res) => {
					debugger
					this.fileList.push({
						name: res.newFileName,
						url: filePreviewUrl + res.fileName
					});
					var fileStr = this.listToString(this.fileList)

					this.$emit("input", fileStr);

					console.log("11111111", fileStr)

					this.$emit("refreshFile");
					uni.hideLoading();
				}).catch(errors => {
					debugger
					var fileStr = this.listToString(this.fileList)

					this.$emit("input", fileStr);

					this.$emit("refreshFile");
					uni.hideLoading();

					console.log(this.fileList, fileStr)
					console.log("2222222222", fileStr)

				})
			},
			// 获取上传进度
			progress(e) {
				console.log('上传进度：', e)
			},

			// 上传成功
			success(e) {

				console.log('上传成功')

			},
			removeFile(e) { //删除文件

				this.fileList.splice(e.index, 1)
				var fileStr = this.listToString(this.fileList)

				this.$emit("input", fileStr);
			},
			// 上传失败
			fail(e) {
				alert("上传失败")
				console.log('上传失败：', e)
			}
		}
	}
</script>

<style>

</style>